package com.service;

import com.mapper.BrandMapper;
import com.pojo.Brand;
import com.util.factory;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;


import java.util.List;
public class BrandService {
    public static void main(String[] args) {

    }

    //        调用brandmapper的方法
    SqlSessionFactory sqlSessionFactory = factory.getSqlSessionFactory();


//                    查询所有
    public List<Brand> selectAll(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
//        获取brandmapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
//          调用查询的方法
        List<Brand> brands = mapper.selectAll();

        return brands;
    }


//               添加数据
    public void add(Brand b){
        SqlSession sqlSession = sqlSessionFactory.openSession();
//        获取brandmapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
//        添加
        mapper.add(b);


        sqlSession.commit();
        sqlSession.close();
    }

//               根据id查询
    public Brand update(int id){
        SqlSession sqlSession = sqlSessionFactory.openSession();
//        获取brandmapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

//        查询数据，作用，数据回显
        Brand brand = mapper.selectID(id);

        return brand;
    }


    //               修改数据
    public void update2(Brand b){
        SqlSession sqlSession = sqlSessionFactory.openSession();
//        获取brandmapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
//        修改
        mapper.update(b);

        sqlSession.commit();
        sqlSession.close();
    }

//    根据id删除
    public void delete(int id)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession();
//        获取brandmapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.out(id);
        sqlSession.commit();
        sqlSession.close();
    }
}
